package cn.gong.moviekong.logic;

import java.util.Set;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.KeyEvent;
import cn.gong.moviekong.manager.ActivityManager;
import cn.gong.moviekong.model.Task;
import cn.gong.moviekong.ui.R;

public class MainService extends Service implements Runnable {
	public static int screenWidth;
	public static int screenHight;
	public static float density;
	public static String cacheDir;
	public static String fileDir;
	private boolean isrun;
	public static boolean serviceState = false;
	public static String manufacturer;
	public static String release;
	public static String model;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	public void run() {
	}

	private void doTask(Task task) {
	}

	public Handler handler = new Handler() {
		public void handleMessage(Message msg) {
		}
	};

	@Override
	public void onDestroy() {
		super.onDestroy();
		isrun = false;
		serviceState = false;
	}

	// 网络连接设置提示
	public static void alertNetError(final Context context) {
		AlertDialog.Builder ab = new AlertDialog.Builder(context);
		ab.setIcon(R.drawable.error);

		ab.setTitle("网络连接设置");
		ab.setMessage("现在没有可用网络，请设置网络或退出程序");
		ab.setPositiveButton("设置网络", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				// 启动网络设置窗口
				Intent it = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
				context.startActivity(it);
			}
		});
		ab.setNegativeButton("退出程序", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// 退出程序
				// 停止服务
				Intent it = new Intent(context, MainService.class);
				context.stopService(it);
				ActivityManager manager = ActivityManager.getInstance();
				// 关掉Activity
				Set<Integer> keySet = manager.getActivityKeySet();
				for (Integer key : keySet) {
					manager.getActivity(key).finish();
				}
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		ab.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					return true;
				}
				return false;
			}
		});
		ab.create().show();
	}

	// 退出提示
	public static void alertExit(final Context context) {
		AlertDialog.Builder ab = new AlertDialog.Builder(context);
		// ab.setIcon(R.drawable.omg);
		AlertDialog.Builder ab1 = new AlertDialog.Builder(context);
		ab.setTitle("提示");
		ab.setMessage("确定退出个性网吗?");
		// ab.setPositiveButton("取消", null);
		ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Intent it = new Intent(context, MainService.class);
				context.stopService(it);

				ActivityManager manager = ActivityManager.getInstance();
				// 关掉Activity
				Set<Integer> keySet = manager.getActivityKeySet();
				for (Integer key : keySet) {
					manager.getActivity(key).finish();
				}
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		ab.setNegativeButton("取消", null);
		ab.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					return true;
				}
				return false;
			}
		});
		ab.create().show();
	}

}
